﻿#include "faceproject.h"
#include "ui_faceproject.h"
#include "facecollect.h"
#include "querywin.h"
#include "QDebug"
#include <QSqlRecord>
#include <QTime>
#include "alterinfo.h"

faceProject::faceProject(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::faceProject)
{
    ui->setupUi(this);
    //导入haarcascade_frontalface_alt2.xml--人脸特征文件
    cascade.load("./haarcascade_frontalface_alt2.xml");
    info.mtime=this->startTimer(100);
    cap.open(0);//打开摄像头
}

faceProject::~faceProject()
{
    delete ui;
    this->killTimer(info.mtime);
}
void faceProject::timerEvent(QTimerEvent *e)
{


    Mat imageMat;
    cap>>imageMat; //cap.read(imageMat);
    //放缩
    Mat srcMat;
    cv::resize(imageMat, srcMat, Size(ui->facelb->width(), ui->facelb->height())); //设置摄像头采集的画面和标签的大小一样

    Mat grayMat;
    cvtColor(srcMat, grayMat, CV_BGR2GRAY);//转灰度图--二值化
    //检测人脸
    std::vector<Rect> faceRects;
    cascade.detectMultiScale(grayMat, faceRects, 1.1,3,0, Size(100,100));


    if(faceRects.size()>0)
    {
        //绘制矩形框
        rectangle(srcMat,faceRects[0],Scalar(255,0,0));
        face = grayMat(faceRects[0]);

        //识别

        Ptr<FaceRecognizer> model =EigenFaceRecognizer::create();
        cv::String file = "./face.xml";
        model->read(file);
        if(face.data == nullptr) return;
        cv::resize(face, face, Size(100,100));        
        info.faceID= model->predict(face);
        qDebug()<<info.faceID;

        //查找数据库里faceID对应的人
        QString select = "select * from faceInfo";
        QString select_where = QString("select * from faceInfo where faceID='%1'").arg(info.faceID);

        QSqlQuery query;
        if(!query.exec(select_where))
        {
            qDebug()<<query.lastError().text();
        }

        if(query.next()) //指向下一条记录
          {
              QSqlRecord record = query.record();
              info.name=record.value(1).toString();
              info.picterpath=record.value(3).toString();
              info.time=record.value(4).toString();
              int signlFlage =record.value(5).toInt();

               if(signlFlage==0) //判断是否签到
               {
                   //保存图片
                   if(imwrite(info.picterpath.toStdString(), face))
                   {
                      QString signltime= QTime::currentTime().toString("hh:mm:ss");

                       //修改签到时间到数据库
                       QString updataSql = QString("update faceInfo set signltime='%1' where name ='%2'").arg(signltime).arg(info.name);
                       if(!query.exec(updataSql))
                         {
                                 qDebug()<<query.lastError().text();
                         }

                       QString updataflage=QString("update faceInfo set flage='%1' where name ='%2'").arg(1).arg(info.name);
                       if(!query.exec(updataflage))
                         {
                                 qDebug()<<query.lastError().text();
                         }
                       QString hint=QString("%1,签到成功！").arg(info.name);
                       ui->textBrowser->append(hint);
                   }

               }
               else {

               }

     }else {
             ui->textBrowser->clear();
             ui->textBrowser->append("对不起你不是本公司人");           

           }
      }

    QString nowtime = QTime::currentTime().toString("hh:mm:ss");
    ui->tiamelb->setText(nowtime);
    QImage image(srcMat.data, srcMat.cols, srcMat.rows, srcMat.step1(), QImage::Format_RGB888);
    ui->facelb->setPixmap(QPixmap::fromImage(image));


}

void faceProject::on_collectbt_clicked()
{
    faceCollect *win = new faceCollect(this);
    win->show();
    this->hide();
    this->killTimer(info.mtime);
}

void faceProject::on_usrbt_clicked()
{
    alterInfo  *win = new alterInfo(this);
    win->show();
    this->hide();
    this->killTimer(info.mtime);
}

void faceProject::on_querybt_clicked()
{
    queryWIN  *win = new queryWIN(this);
    win->setUsrInfo(info.name,info.picterpath,info.time);
    win->show();
}
